
class JQWxAppletSession extends global.JQService {
    async do_action(){
		
		// const appid ='wx2173929dde29c002';
		// const secret = 'bdc69b0aa9c5ec895be5521a0632fb1b';
		
			
		const appid =global.consts.Weixin.APPLET_ID;
		const secret = global.consts.Weixin.APPLET_SECRET;
		
		console.log(JSON.stringify(this.req_obj));
		let data_obj = global.JQRequest.parseFromData(this.req_obj.data);
		console.log('login session oooo1111');              
		// appid, secret, code,isMA
		let obj = await global.JQNetwork.JQOAuth2.loginWeixin(appid,secret,data_obj.code,true);
		if(obj && obj.errcode){
			throw {'code':global.consts.Error.ERROR_OAUTH,
			'message':global.consts.Message.ERROR_OAUTH}
		}
		let visitor_table = global.JQTableIF.instance('visitor_table',this.conn);
		console.log('visitor_table table name'+!visitor_table);
		let visitor_obj = await visitor_table.queryOne(null,[['unique_code=',obj.openid],['type=',1]
			]);
		console.log('visitor_table obj ' +JSON.stringify(visitor_obj));
		if(!visitor_obj){
			let create_obj = {'unique_code':obj.openid,'type':1};
			visitor_obj = await visitor_table.create(create_obj);
		}	
		if(!visitor_obj)return null;
		console.log('login session 1111');
		let session_table = global.JQTableIF.instance('login_session_table',this.conn);
		if(!session_table)return null;
			console.log('login session 22222');
		let session_obj = await session_table.queryOne(null,
			[['visitor_id=',visitor_obj.id]]);
		if(session_obj){
			await session_table.remove(session_obj['id']);
			delete session_obj['id'];
			delete session_obj['login_time'];
			
		}else{
			session_obj={};
			session_obj.visitor_id = visitor_obj.id;			
		}
		console.log('login session 3333');
		
		session_obj.session_token=global.JQGuid.generate();
		session_obj = session_table.create(session_obj,{'login_time':'now()'});
		return session_obj;			
	}

}
module.exports = JQWxAppletSession;

